package com.study.rabbitmq.three;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.study.rabbitmq.util.RabbitUtil;
import com.study.rabbitmq.util.SleepUtil;

import java.io.IOException;

/**
 * @Author chenyeqing
 * @Date 2021/9/2 22:36
 */
public class Work04 {

    /**
     * 队列名称
     */
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws IOException {
        Channel channel = RabbitUtil.getChannel();
        System.out.println("C2 等待接受消息处理时间较长");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            // 沉睡 1 秒
            SleepUtil.Sleep(30);
            System.out.println(new String(message.getBody()));

            // 手动应答
            /**
             * 1. 消息的标记 tag
             * 2. 是否批量应答
             *      false：不批量应答信道中的消息
             *      true：批量
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        };
        // 设置不公平分发prefetchCount
        // 表示 一个消费者最多只能一次拉取N条消息
        int prefetchCount = 5;
        channel.basicQos(prefetchCount);
        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME, autoAck, deliverCallback, cancelCallback);
    }
}
